<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Email extends CI_Controller{
	/**
     * 构造函数
     *
     * @access  public
     * @return  void
     */
	public function __construct(){
		parent::__construct();
		$this->user_name = $this->session->userdata('user_name');
		$this->user_id = $this->session->userdata('user_id');
	}

	function get_jsonp_in_count(){
		//$username = $this->input->get('username');
		//$username = urldecode($username);		
		$userid = $this->input->get('userid');
		
		$this->db->where('userid', $userid);
		$this->db->where('read', '0');
 		$this->db->where_in('email.delete', array('0', '4'));
		$query=$this->db->get('email');
		$content = $query->num_rows();
		$arrset['result'] = $content;
		$result = json_encode($arrset); 
		$callback=$this->input->get('callback');
		echo $callback."($result)"; 
	}

	//截取固定长度的标题
	function mysub($str,$start=0,$length=60,$left="...",$charset="utf-8"){
		if($charset=="utf-8"){
			$index=0; //当前字符的第一字节位置
			$index1=0; //当前字符的最后字节位置
			$result="";
			$haslen=0; //已经截取的字节
			for($i=0;$i<$length;$i++){
				$index1=$index;
				$len=0;
				$len1=0;
				$first_b=substr($str,$index,1);
				if(ord($first_b)>224){
					if($i>=$start){
						$len=3;
						$len1=3;
					}
					$index+=$len;
					$haslen+=$len;
				 }elseif(ord($first_b)>192){
					if($i>=$start) {
						$len=2;
						$len1=2;
					}
					$index+=$len;
					$haslen+=$len;
				}else{
					if($i>=$start) {
						$len=1;
						$len1=1.5; //这里引文显示的时候2个是一个中文的长度所以我们定为1.5因为中文是3
					}
					$index+=$len;
					$haslen+=$len1;
				 }
				 if($haslen<=$length){
					$result.=substr($str,$index1,$len);
				 }else{
					break;
				 }
			}
		}else{
			$result=mb_substr($str,$start,$length,$charset);
		}
		if($result!=$str){
		   $result.=$left;
		}
		return $result;
	}

	//收件箱方法
	function get_jsonp_in_list(){
		$content = array();
		//$username = $this->input->get('username');
		//$username = urldecode($username);
		
		$userid = $this->input->get('userid');		
		$StartNum = $this->input->get('page');
		//$username = $this->input->get('username');
		//$StartNum = $this->input->get('StartNum'); 
		$pagesize=10;
		if($StartNum < 0){
			$StartNum = 1;
		}
		$start=($StartNum-1)*$pagesize;
		$this->db->select('email.emailid,email.read,email_body.sender,email_body.title,email_body.sendertime,email_body.bodyid,email_body.attachment_id');
		$this->db->join('email_body', 'email_body.bodyid = email.bodyid');
		$this->db->where('email.userid', $userid);
		
		$names = array('0', '4');
 		$this->db->where_in('email.delete', $names);
		$this->db->order_by("email.emailid", 'desc'); 
		$this->db->limit($pagesize,$start);
		$query=$this->db->get('email');
		//echo $this->db->last_query();//输出随后一次查询的语
		foreach ($query->result() as $v){
			$st = reset(explode(' ',$v->sendertime));
			$isa = $v->attachment_id?1:0;
			$content[] = array("id"=>$v->emailid,"s"=>$v->sender,"t"=>$this->mysub($v->title,0,51),"r"=>$v->read,"st"=>$st,"bodyid"=>$v->bodyid,'isa'=>$isa);

		}
		$arrset['result'] = $content;
		//$arrset['a_count'] = count($content);
		//$arrset['startNum'] = ++$StartNum;
		
		$result = json_encode($arrset);
		//$callback=$_GET['jsoncallback'];  
		$callback=$this->input->get('callback');
		echo $callback."($result)";  
	}
	
	
	function get_jsonp_in_content(){
		//标记为已读
		$emailid = $this->input->get('emailid');
		$data = array('read' => '1','readtime' => date("Y-m-d H:i:s",time()));
		$this->db->where('emailid', $emailid);
		$this->db->update('email', $data); 
		
		//取详细内容
		$bodyid = $this->input->get('bodyid');
		//$this->db->where('bodyid', $bodyid);
		//$this->db->limit(1);
		$query=$this->db->where('bodyid', $bodyid)->limit(1)->get('email_body');
		
		$row = $query->first_row();
		$content = strip_tags($row->content,"<br>");
		$arrset = array(
			"id"=>$row->bodyid,
			"senderid"=>$row->senderid,
			"sender"=>$row->sender,
			"addressee"=>$row->addressee2,
			"title"=>$row->title,
			"content"=>$content,
			"sendertime"=>$row->sendertime
		);

		if($row->attachment_id != ''){
			$moduletype = "email";
			$attach_id = substr($row->attachment_id,0,-1);
			$attach_name = substr($row->attachment_name,0,-1);
			$arrset['attach_id'] = explode(",",$attach_id);
			$arrset['attach_name'] = explode("*",$attach_name);
		}  
		
		$result = json_encode($arrset);
		//$callback=$_GET['jsoncallback'];  
		$callback=$this->input->get('jsoncallback');
		echo $callback."($result)"; 

	}//end 收件箱方法结束
	
	//发件箱方法
	function get_jsonp_out_list(){
		$content = array();
		//$username = $this->input->get('username');
		//$username = urldecode($username);
		$userid = $this->input->get('userid');
		$StartNum = $this->input->get('page'); 
		$pagesize=10;
		if($StartNum < 0){
			$StartNum = 1;
		}
		$start=($StartNum-1)*$pagesize;
		$this->db->select('email.delete,email_body.sender,email_body.bodyid,email_body.title,email_body.sendertime,email_body.attachment_id');
		$this->db->join('email_body', 'email_body.bodyid = email.bodyid','right');
		$this->db->where('email_body.senderid', $userid);
		
		$names = array('0', '1');
 		$this->db->where_in('email.delete', $names);
		$this->db->group_by('email_body.bodyid'); 
		$this->db->order_by("email_body.bodyid", 'desc'); 
		$this->db->limit($pagesize,$start);
		$query=$this->db->get('email');
		//echo $this->db->last_query();//输出随后一次查询的语
		foreach ($query->result() as $v){
			$st = reset(explode(' ',$v->sendertime));
			$isa = $v->attachment_id?1:0;
			$content[] = array("bodyid"=>$v->bodyid,"t"=>$this->mysub($v->title,0,51),"st"=>$st,'isa'=>$isa);

		}
		$arrset['result'] = $content;
		//$arrset['a_count'] = count($content);
		//$arrset['startNum'] = ++$StartNum;
		
		$result = json_encode($arrset);
		//$callback=$_GET['jsoncallback'];  
		$callback=$this->input->get('callback');
		echo $callback."($result)";  
	}
	
	function get_jsonp_out_content(){
		$bodyid = $this->input->get('bodyid');
		$this->db->where('bodyid', $bodyid);
		$this->db->limit(1);
		$query=$this->db->get('email_body');
		
		$row = $query->first_row();
		$content = strip_tags($row->content,"<br>");
		$arrset = array(
			"id"=>$row->bodyid,
			"sender"=>$row->sender,
			"addressee"=>$row->addressee2,
			"title"=>$row->title,
			"content"=>$content,
			"sendertime"=>$row->sendertime
		);

		if($row->attachment_id != ''){
			$moduletype = "email";
			$attach_id = substr($row->attachment_id,0,-1);
			$attach_name = substr($row->attachment_name,0,-1);
			$arrset['attach_id'] = explode(",",$attach_id);
			$arrset['attach_name'] = explode("*",$attach_name);
		}  
		
		$result = json_encode($arrset);
		//$callback=$_GET['jsoncallback'];  
		$callback=$this->input->get('jsoncallback');
		echo $callback."($result)"; 

	}//end 发件箱方法结束

}